<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<meta name="viewport" content="width=device-width,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="format-detection" content="telephone=no">
		<title>江苏绿地会  积分兑换</title>
		<link href="{$Think.config.static_path}duihuan/css/main.css" rel="stylesheet" type="text/css">
		<style type="text/css">
			.hide{
				display: none;
			}
			#loading{
				text-align:center;
				position:fixed;
				top:45%;
				height:100%;
				left:50%;
			}
		</style>
	</head>
	<body class="bg layout">
		<div class="bg-white">
			<div class="content">
            <empty name="isexhange">
				<div class="address">
					<h3>请选择您要兑换商品的地址</h3>
					<select id="city"  onChange="change('')">
						<option value="0">--请选择--</option>
                        <volist name="list" id="val">
							<option value="{$val.id}">{$val.title}</option>
                        </volist>
		
					</select>
					<p>仅可在选取地点领取兑换商品。</p>
					<!-- <h3>您要兑换商品的地址</h3>
					<h3 style="padding:10px 0">徐州</h3>
					<p>仅可在选取地点领取兑换商品。</p> -->
				</div>
                <else/>
              		<div class="address"> <h3>您要兑换商品的地址</h3><h3 style="padding:10px 0">{$storeName}</h3><p>仅可在选取地点领取兑换商品。</p></div>
                </empty>
				<div class="line1">
					<div class="border"></div>
					<div class="">
						<div class="ruleimg">
							<img src="{$Think.config.static_path}/duihuan/images/icon1.png" class="icon2">
						</div>
						<div class="prize">
							<div>
								<table>
									<thead>
										<th>商品</th>
										<th>市场价</th>
										<th>会员兑换价</th>
										<th>兑换</th>
									</thead>
									<tbody id="list">
                                    <volist name="data" id="val">
										<tr>
											<td>{$val.title}</td>
											<td>{$val.price}元</td>
											<td><span class="grade">{$val.score}</span>积分</td>
											<td>
                                            	<if condition="$val['num'] egt $val['total']">
                                                	<span class="exchange gray"  >已兑完</span>
                                                <else/>
													<span class="exchange" onClick="exchange(this,'{$val.sid}','{$val.id}','{$val.title}')" >我要兑换</span>
                                                </if>
											</td>
										</tr>
                                        </volist>
										
									</tbody>
								</table>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</body>
    <include file="public:share" />
	<script type="text/javascript" src="{$Think.config.static_path}/jquery-2.1.4.min.js"></script>
	<script type="text/javascript" src="{$Think.config.static_path}duihuan/js/util.js"></script>
	<script type="text/javascript">
		$(function(){
			if("{$isexhange}"){
				change("{$sid}");
			}/*else{
				$.getJSON("__URL__/checkUser",function(json){
					if(json.status==0){
						Util.info(json.info);
						$("#city").prop('disabled',true);
						$(".exchange").addClass("gray");
					}
				})
			}*/
		})
		
		
		function change(sid) {
			var sid = sid ? sid : $("#city  option:selected").val() == 0 ? 1  : $("#city  option:selected").val();
			var html = '',
			str = '';
			Util.ajax({
				method: 'post',
				data: 'sid=' + sid+'&openid={$openid}',
				url: '__URL__/ajaxProduct'
			},
			function(json) {
				if (json.status > 0) {
					var len = json.data.length;
					$.each(json.data,function(i, val) {
						//alert(json.data[i]['title']);
						//alert(typeof parseInt(val.num));
						if(parseInt(val.num) >= parseInt(val.total) && val.code=='我要兑换'){
							str = '<span class="exchange gray"  >已兑完</span>';
						}else{
							if (val.isexchange == 0) {
								str = '<span class="exchange" id=p'+val.id+' onClick=exchange(this,"' + val.sid + '","' + val.id + '","' + val.title + '") >我要兑换</span>';
							} else {
								if(val.code!='我要兑换'){
									str = '<span class="exchange">' + val.code + '</span>';
								}else{
									str = '<span class="exchange gray">' + val.code + '</span>';
								}
							}
						}
							
						html += '<tr><td>' + val.title + '</td><td>' + val.price + '元</td><td><span class="grade">' + val.score + '</span>积分</td><td>' + str + ' </td></tr>';
					})
				} else {
					html = '<tr><td colspan=4>' + json.data + '</td></tr>'
				}
				$("#list").html(html);
			});
			
			
		}
		var  tmp =true;
		function exchange(obj, sid, pid, title) {
			if (!$(obj).hasClass("gray")) {
				var city = $("#city  option:selected").val();
				var cityName = $("#city  option:selected").text();
				if (parseInt(city) == 0) {
					Util.info("请选择您要兑换商品的地址");
					return;
				}
				var grade = $(obj).parents("tr").find(".grade").text();
				if(tmp){
					Util.showmsg("您确定兑换么，兑换消耗" + grade + "积分",
						function exchanges() {
							tmp =false;
							Util.ajax({
								method: 'post',
								data: 'sid=' + $("#city  option:selected").val() + '&pid=' + pid + '&title=' + title + '&openid={$openid}',
								url: '__URL__/exchange'
							},
							function(json) {
								if (json.status > 0) {
									$(".exchange").addClass("gray");
									$("#p"+json.pid).html(json.key);
									$("#p"+json.pid).removeClass('gray');
									var html = '<h3>您要兑换商品的地址</h3><h3 style="padding:10px 0">' + cityName + '</h3><p>仅可在选取地点领取兑换商品。</p>';
									$(".address").html(html);
								} else {
									Util.info(json.info);
									tmp=true;
								}
							})
							tmp =false;
						},
					function tt(){tmp=true;}, "confirm");
					tmp =false;
				}
			}
		}
	</script>
</html>